package cn.wsalix.msg.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Observable;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.anlaser.service.impl.CommonServiceImpl;
import cn.anlaser.utils.SearchFilter;
import cn.anlaser.utils.SearchFilter.Operator;
import cn.wsalix.msg.entity.MessRapport;
import cn.wsalix.msg.entity.Message;
import cn.wsalix.msg.form.MessageForm;
import cn.wsalix.msg.repository.MessRapportRepository;
import cn.wsalix.msg.service.MessRapportService;
import cn.wsalix.observable.service.SendService;
import cn.wsalix.type.MessTypeEnum;
import cn.wsalix.user.entity.Mine;
import cn.wsalix.user.form.MessRapportForm;
import cn.wsalix.user.service.MineService;

@Service("messRapportService")
public class MessRapportServiceImpl extends
		CommonServiceImpl<MessRapportForm, MessRapportRepository, MessRapport>
		implements MessRapportService {
	@Autowired
	private MessRapportRepository messRapportRepository;
	@Autowired
	private MineService myProductService;
	@Override
	public MessRapportRepository getRepository() {
		return messRapportRepository;
	}

	@Override
	public Map<String, SearchFilter> buildByForm(MessRapportForm form) {
		Map<String, Object> searchParams = new HashMap<String, Object>();
		Map<String, SearchFilter> filters = SearchFilter.parse(searchParams);
		if (form.getTitle() != null && !form.getTitle().trim().equals("")) {
			filters.put("title",
					new SearchFilter("title", Operator.LIKE, form.getTitle()));
		}
		if (form.getType() != null) {
			filters.put("type",
					new SearchFilter("type", Operator.EQ, form.getType()));
		}
		if (form.getFkId() != null && form.getFkId() != 0) {
			filters.put("fkId",
					new SearchFilter("fkId", Operator.EQ, form.getFkId()));
		}
		if (form.getUserId() != null && form.getUserId() != 0) {
			filters.put("receive.id",
					new SearchFilter("receive.id", Operator.EQ, form.getUserId()));
		}
		if (form.isQueryReaded()) {
			if (form.isReaded()) {
				filters.put("readed", new SearchFilter("readed", Operator.EQ,
						true));
			} else {
				filters.put("readed", new SearchFilter("readed", Operator.EQ,
						false));
			}
		}
		return filters;
	}

	@Override
	public String module() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Class<MessRapport> getEntityClazz() {
		return MessRapport.class;
	}

	@Override
	public List<Message> findProMessByForm(MessRapportForm form) {
		return messRapportRepository.findProMess(form.getUserId(),
				form.getFkId());
	}
	@Transactional
	@Override
	public void update(Observable o, Object arg) {
		if (o instanceof SendService) {
			SendService sendService = (SendService) o;
			if (arg instanceof MessageForm) {
				Message message = sendService.getMessage();
				switch (message.getType()) {
				case order:
					break;
				case product:
					List<Mine> resLst = myProductService
							.findByProductId(message.getFkId());
					for (Mine myProduct : resLst) {
						MessRapport messRapport = new MessRapport();
						messRapport.setTitle(message.getTitle());
						messRapport.setReaded(false);
						messRapport.setType(MessTypeEnum.product);
						messRapport.setFkId(message.getFkId());
						messRapport.setSendTime(new Date());
						messRapport.setMessage(message);
						messRapport.setReceive(myProduct.getUser());
						
						super.add(messRapport);
					}

					break;
				case tuan:
					break;
				default:
					break;
				}
			}
		}
	}

	@Override
	public boolean sendMess(MessRapportForm form) {
		MessRapport messRapport =messRapportRepository.findOne(form.getId());
		messRapport.setReaded(true);
		super.edit(messRapport);
		return true;
	}

}
